package ordinaryArray;

import org.junit.Test;

import java.util.Arrays;

/*
 *@description
 *@author hkfan
 *@version 1.0
 *@create 2025/5/26 15:56
 */
public class LeetCode_274 {

    @Test
    public void test(){
        System.out.println(hIndex(new int[]{3,0,6,1,5}));
    }

    public int hIndex(int[] citations) {
        //1. 排序
        Arrays.sort(citations);
        //2. 定义遍历,循环处理
        int res = 0,k=0;
        for (int i = 1; i <= citations.length; i++) {
            int j = findIndex(citations,k,i);
            k=j;
            if (j<0){
                break;
            }
            if (citations.length-j>=i){
                res = i;
            }else {
                break;
            }
        }
        return res;
    }

    private int findIndex(int[] citations, int k,int val) {
        for (int i = k; i < citations.length; i++) {
            if (citations[i]>=val){
                return i;
            }
        }
        return -1;
    }
}
